<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h2>sessionStorage</h2>
    <button onClick="saveData()">保存数据</button>
    <button onClick="readData()">读取数据</button>
    <button onClick="deleteData()">删除数据</button>
    <button onClick="deleteAllData()">删除所有数据</button>
</body>
<script>
    let p = {
        name: 'SONG',
        age: 18
    }//无法存对象
    // sessionStorage浏览器关闭就清空
    // 4个方法 
    function saveData() {
        // 第二个参数必须是字符串
        sessionStorage.setItem('msg', 'msg1')
        sessionStorage.setItem('person', JSON.stringify(p))
    }
    function readData() {
        console.log(sessionStorage.getItem('msg'))
        console.log(sessionStorage.getItem('person'))

        const result = sessionStorage.getItem('person')
        console.log(JSON.stringify(result))
    }//读取数据没有的话null
    function deleteData() {
        sessionStorage.removeItem('msg')
    }
    function deleteAllData(){
        sessionStorage.clear()
    }
</script>

</html>